package oocl.eshop.service.impl;

import java.util.List;

import oocl.eshop.dao.MerchantDao;
import oocl.eshop.dao.PermissionDao;
import oocl.eshop.dao.impl.MerchantDaoImpl;
import oocl.eshop.dao.impl.PermissionDaoImpl;
import oocl.eshop.po.Merchant;
import oocl.eshop.po.Permission;
import oocl.eshop.service.MerchantManager;

public class MerchantManagerImpl implements MerchantManager {
	MerchantDao merchants = new MerchantDaoImpl();
	PermissionDao pers = new PermissionDaoImpl();

	@Override
	public List<Merchant> findAllMerchant() {
		List<Merchant> mers = merchants.findAllMerchants();
//		if (!mers.isEmpty()) {
//			System.out.println(" person");
//		}
		for (Merchant merchant : mers) {
			merchant.setmStatus(pers.findPermisson(merchant.getmId()).getmStatus());
		}
		return mers;
	}

	@Override
	public int addMerchant(Merchant merchant) {
		// TODO Auto-generated method stub
		int m=merchants.addMerchant(merchant);
		if (m!=0) {
			int mId=merchants.findMerchantByTel(merchant.getmTel()).getmId();
			m=pers.addPermisson(new Permission(mId, 1));
			if (m==0) {
				merchants.deleteMerchant(mId);
			}
		}
		return m;
	}

}
